<?php
/**
 * 个人管理控制器
 */

class PersonalAction extends BaseAction {
    public function __construct() {
        parent::__construct();

        $this->breadcrumbsPush('个人', U('Personal/index'));
        $this->subnavs = array(
            array(
                'name' => '个人',
                'url' => U('Personal/index'),
                'action' => 'index',
                'icon' => 'user',
            ),
            array(
                'name' => '信息',
                'url' => U('Personal/information'),
                'action' => 'information',
                'icon' => 'credit-card',
            ),
            array(
                'name' => '编辑',
                'url' => U('Personal/profile'),
                'action' => 'profile',
                'icon' => 'edit',
            ),
            array(
                'name' => '修改密码',
                'url' => U('Personal/repassword'),
                'action' => 'repassword',
                'icon' => 'unlock-alt',
            ),
        );
    }

    public function information() {
        $this->breadcrumbsPush('信息', '');
        $this->display();
    }

    public function setting() {
        $this->breadcrumbsPush('设置', '');
        $this->display();
    }

    public function profile() {
        $this->breadcrumbsPush('编辑', '');
        if ($this->_post()) {
            $model = D('Member');
            if ($model->create()) {
                if ($model->save()) {
                    $_SESSION['user'] = $this->user = D('Member')->where(array(
                        'id' => $this->user['id']))->relation(true)->find();
                }
                else $error = '修改失败';
            }
            else $error = $model->getError();

            if (isset($error)) {
                $this->alert = array(
                    'type' => 'error',
                    'info' => $error
                );
            }
            else {
                $this->alert = array(
                    'type' => 'success',
                    'info' => '修改成功'
                );
            }
        }
        $this->display();
    }

    public function repassword() {
        $this->breadcrumbsPush('修改密码', '');
        if ($this->_post()) {
            $password = M('Member')->where(array(
                'id' => $this->user['id']
            ))->getField('password');
            if ($password == $this->_post('password', md5)) {
                $repassword = $this->_post('repassword', md5);
                $repassword2 = $this->_post('repassword2', md5);
                if ($repassword == $repassword2) {
                    M('Member')->where(array(
                        'id' => $this->user['id']
                    ))->save(array(
                        'password' => $repassword
                    ));
                }
                else $error = '新密码不相同';
            }
            else $error = '原始密码错误';

            if (isset($error)) {
                $this->alert = array(
                    'type' => 'error',
                    'info' => $error
                );
            }
            else {
                $this->alert = array(
                    'type' => 'success',
                    'info' => '修改成功'
                );
            }
        }
        $this->display();
    }
}
